package com.gyt.manager.common.utils;

import com.gyt.manager.common.dto.EmailDTO;
import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * description.
 *
 * @author gyt create on 2021/5/10 13:17
 */
@Component
public class EmailUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmailUtil.class);

    //private static final String FROM = "961920261@qq.com";

    private static final String HOST = "smtp.qq.com";

    private static final String THIRD_USE_CODE = "bhdnjiraqsllbfbd";

    public static Boolean  sendEmail(EmailDTO emailDTO){
        boolean flag = false;
        try {
            Properties properties = System.getProperties();
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

            //设置系统属性
            properties.setProperty("mail.smtp.host", HOST);
            properties.put("mail.smtp.auth", "true");
            //获取发送邮件会话、获取第三方登录授权码
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailDTO.getFrom(), THIRD_USE_CODE);
                }
            });

            Message message = new MimeMessage(session);

            //防止邮件被当然垃圾邮件处理，披上Outlook的马甲
            message.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");

            message.setFrom(new InternetAddress(emailDTO.getFrom()));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailDTO.getTo()));

            //邮件标题
            message.setSubject(emailDTO.getTitle());

//            BodyPart bodyPart = new MimeBodyPart();
//
//            bodyPart.setText("我发送了文件给你");
//
//
//            Multipart multipart = new MimeMultipart();
//
//            multipart.addBodyPart(bodyPart);

            //附件
//            bodyPart = new MimeBodyPart();
//            String fileName = "F:\\Person\\qq\\file";
//            DataSource dataSource = new FileDataSource(fileName);
//            bodyPart.setDataHandler(new DataHandler(dataSource));
//            bodyPart.setFileName("文件显示的名称");
//            multipart.addBodyPart(bodyPart);

//            message.setContent(multipart);

            //"text/html;charset=utf-8"
            message.setContent(emailDTO.getContent(), emailDTO.getContentType());
            Transport.send(message);
            flag = true;
        } catch (Exception e) {
            LOGGER.error("send email error-->", e);
        }
        return flag;
    }
}
